package com.software.seakdream.controller;

import com.software.seakdream.domain.Result;
import com.software.seakdream.entity.Integral;
import com.software.seakdream.entity.UserInfo;
import com.software.seakdream.repository.UserInfoRepository;
import com.software.seakdream.service.IntegralService;
import com.software.seakdream.utils.ResultUtils;
import com.xxl.job.core.util.DateUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Calendar;
import java.util.Date;
import java.util.List;

//积分界面Controller
@RestController
@RequestMapping("/Bonus")
public class IntegralController {

    @Autowired
    private IntegralService integralService;

    @Autowired
    private UserInfoRepository userInfoRepository;
    //得到用户的积分
    @GetMapping("/getUserBonus")
    public int getUserBonusById(String userId){
        return userInfoRepository.getPointsById(userId);
    }

    //用户做任务后将任务积分加到用户积分上
    @GetMapping("/addUserBonus/{userId}/{integralId}")
    public int addUserBonus(@PathVariable(value = "userId") String userId,@PathVariable(value = "integralId")Integer integralId) {
        Integer userPoints = integralService.findUserPointById(userId);
        Integer bonus = integralService.findBonusByIntegralId(integralId);
        userInfoRepository.updatePoints(userId,userPoints+bonus);
        return userPoints+bonus;
    }

    //查询所有的任务
    @GetMapping("/getAllTask")
    public List<Integral> getAllIntegral() {
        List<Integral> list = integralService.findAllIntegral();
        for (Integral i:
             list) {
            Calendar calendar1 = Calendar.getInstance();
            calendar1.set(calendar1.get(Calendar.YEAR), calendar1.get(Calendar.MONTH), calendar1.get(Calendar.DAY_OF_MONTH),
                    0, 0, 0);
            Date beginOfDate = calendar1.getTime();
            // 获取当天23点59分59秒Date
            Calendar calendar2 = Calendar.getInstance();
            calendar1.set(calendar2.get(Calendar.YEAR), calendar2.get(Calendar.MONTH), calendar2.get(Calendar.DAY_OF_MONTH),
                    23, 59, 59);
            Date endOfDate = calendar1.getTime();
            Date date = i.getCtime();
            if(i.getCtime() == null){
                System.out.println("不是当天" + i.getCtime());
                Date xdate = new Date();
                i.setIsdone(0);
                updateIntegral(i);
            }
            if(date.getTime() >= beginOfDate.getTime() && date.getTime() <= endOfDate.getTime()){
                System.out.println("就在今天" + i.getCtime());
                i.setIsdone(1);
                updateIntegral(i);
            }
            else {
                System.out.println("不在今天" + i.getCtime());
                i.setIsdone(0);
                updateIntegral(i);
            }


        }
        System.out.println(ResultUtils.success(list));
        return list;
    }
    
    
    //更新任务状态
    @GetMapping("/missionUpdate")
    public boolean missionUpdate(int key){
        List<Integral> list = integralService.findAllIntegral();
        System.out.println(key);
        for (Integral x:
                list) {
            System.out.println("check");
                        if (key == x.getKey()){
                            Calendar calendar1 = Calendar.getInstance();
                            calendar1.set(calendar1.get(Calendar.YEAR), calendar1.get(Calendar.MONTH), calendar1.get(Calendar.DAY_OF_MONTH),
                                    0, 0, 0);
                            Date beginOfDate = calendar1.getTime();
                            // 获取当天23点59分59秒Date
                            Calendar calendar2 = Calendar.getInstance();
                            calendar1.set(calendar2.get(Calendar.YEAR), calendar2.get(Calendar.MONTH), calendar2.get(Calendar.DAY_OF_MONTH),
                                    23, 59, 59);
                            Date endOfDate = calendar1.getTime();
                            Date date = x.getCtime();
                            if(x.getCtime() == null){
                                System.out.println("不是当天111" + x.getCtime());
                                Date xdate = new Date();
                                x.setIsdone(1);
                                x.setCtime(new Date());
                                updateIntegral(x);
                                return true;
                            }
                            if(date.getTime() >= beginOfDate.getTime() && date.getTime() <= endOfDate.getTime()){
                                System.out.println("就在今天111" + x.getCtime());
                                x.setIsdone(1);
                                updateIntegral(x);
                                return false;
                            }
                            else {
                                System.out.println("不在今天111" + x.getCtime());
                                x.setIsdone(1);
                                x.setCtime(new Date());
                                updateIntegral(x);
                                return true;
                            }
//                            x.setCtime(new Date());
//                            x.setIsdone(1);
                        }
        }
        return false;
    }

    //更新Integral
    @GetMapping("/updateIntegral")
    public void updateIntegral(@PathVariable(value = "integral")Integral integral) {
        integralService.updateIntegral(integral);
    }
}
